package com.utils;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.example.demo.context.ContextHolder;
import com.example.demo.context.UserContext;
import com.example.demo.domain.vo.AreaInfo;
import com.example.demo.domain.vo.LoginInfo;
import com.example.demo.domain.vo.LogonUser;
import com.example.demo.domain.vo.MenuResource;
import com.example.demo.domain.vo.SecurityUserService;
import com.utils.catche.SecurityCacheUtils;

public class SecurityUtils {
	private static SecurityUserService securityUserService;
	  private static boolean enabled = false;

	  private static Boolean securityDebug = Boolean.valueOf(false);

	  @Autowired(required=false)
	  public void setSecurityUserService(SecurityUserService securityUserService)
	  {
	    securityUserService = securityUserService;
	    enabled = true;
	  }

	  public static boolean isSecurityDebug()
	  {
	    return securityDebug.booleanValue();
	  }

	  @Value("${sys.security.debug:false}")
	  public void setSecurityDebug(Boolean securityDebug) {
	    securityDebug = securityDebug;
	  }

	  public static LogonUser login(LoginInfo loginInfo)
	  {
	    if (!enabled) {
	      loginInfo.setMessage("安全认证未启用");
	      return null;
	    }

	    if (StringUtils.isBlank(loginInfo.getLoginName())) {
	      loginInfo.setMessage("用户名不能为空");
	      return null;
	    }
	    if (StringUtils.isBlank(loginInfo.getPassword())) {
	      loginInfo.setMessage("密码不能为空");
	      return null;
	    }

	    LogonUser logonUser = securityUserService.login(loginInfo);
	    if (logonUser == null) {
	      if (StringUtils.isBlank(loginInfo.getMessage())) {
	        loginInfo.setMessage("用户无效或密码错误");
	      }
	      return null;
	    }

	    SecurityCacheUtils.cacheUser(logonUser);

	    initUserContext(logonUser);

	    return logonUser;
	  }

	  public static void logout(LogonUser logonUser)
	  {
	    securityUserService.logout(logonUser);
	    SecurityCacheUtils.removeLogonUser(logonUser.getUserKey());
	  }

	  public static List<MenuResource> getRoleResources(Set<String> roleIds)
	  {
	    return securityUserService.getRoleResources(roleIds);
	  }

	  public static UserContext initUserContext(LogonUser logonUser)
	  {
	    UserContext userContext = ContextHolder.getUserContext();
	    userContext.setUserId(logonUser.getUserId());
	    userContext.setUserName(logonUser.getUserName());
	    userContext.setOrganId(logonUser.getOrganId());
	    userContext.setOrganName(logonUser.getOrganName());
	    userContext.setLoginName(logonUser.getLoginName());
	    userContext.setDomainId(logonUser.getDomainId());
	    userContext.setDomainName(logonUser.getDomainName());
	    userContext.setExtContext(logonUser.getExtInfo());

	    if (logonUser.getAreaInfo() != null) {
	      AreaInfo areaInfo = logonUser.getAreaInfo();
	      userContext.setAreaCode(areaInfo.getAreaCode());
	      userContext.setAreaName(areaInfo.getAreaName());

	      userContext.setAreaCode1(areaInfo.getAreaCode1());
	      userContext.setAreaName1(areaInfo.getAreaName1());

	      userContext.setAreaCode2(areaInfo.getAreaCode2());
	      userContext.setAreaName2(areaInfo.getAreaName2());

	      userContext.setAreaCode3(areaInfo.getAreaCode3());
	      userContext.setAreaName3(areaInfo.getAreaName3());

	      userContext.setAreaCode4(areaInfo.getAreaCode4());
	      userContext.setAreaName4(areaInfo.getAreaName4());

	      userContext.setAreaCode5(areaInfo.getAreaCode5());
	      userContext.setAreaName5(areaInfo.getAreaName5());

	      userContext.setAreaCode6(areaInfo.getAreaCode6());
	      userContext.setAreaName6(areaInfo.getAreaName6());
	    }
	    return userContext;
	  }

	  public static List<MenuResource> getMenuResource(String roleKey, String parentId, boolean recursive)
	  {
	    return SecurityCacheUtils.getCachedRoleMenus(roleKey, parentId, Boolean.valueOf(recursive));
	  }

	  public static List<MenuResource> getMenuResource(LogonUser logonUser, String parentId, boolean recursive)
	  {
	    return SecurityCacheUtils.getCachedRoleMenus(logonUser.getRoleKey(), parentId, Boolean.valueOf(recursive));
	  }

	  public static LogonUser getLogonUser(String userKey)
	  {
	    return SecurityCacheUtils.getLogonUser(userKey);
	  }

	  public static boolean authUrl(LogonUser logonUser, String url)
	  {
	    if ((isSecurityDebug()) || (StringUtils.isBlank(url)) || ("/".equals(url))) {
	      return true;
	    }

	    return SecurityCacheUtils.checkRoleUrl(logonUser.getRoleKey(), url);
	  }

	  public static boolean authUrl(LogonUser logonUser, String menuId, String url)
	  {
	    if ((isSecurityDebug()) || (StringUtils.isBlank(url)) || ("/".equals(url))) {
	      return true;
	    }
	    String parentUrl = SecurityCacheUtils.getMenuUrl(menuId);
	    if (parentUrl == null) {
	      return false;
	    }

	    return SecurityCacheUtils.checkRoleUrl(logonUser.getRoleKey(), parentUrl);
	  }

	  public static void refreshUser(String userId)
	  {
	    SecurityCacheUtils.clearLogonUser(userId);
	  }

	  public static void refreshRole(String roleId)
	  {
	    SecurityCacheUtils.clearRoleCache(roleId);
	  }
}
